package com.example.guiguiapplicatio.fragment.followup;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.guiguiapplicatio.api.Interface.Api;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentFollowUpRecordClassifyBinding;
import com.example.guiguiapplicatio.fragment.followup.adapter.FollowUpRecordClassifyFragmentAdapter;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class FollowUpRecordClassifyFragment extends Fragment {

    private FragmentFollowUpRecordClassifyBinding binding;
    private String idcard;
    private Retrofit retrofit;
    private Api api;
    private OkHttpClient client;
    private List<String> NOW_DATA = new ArrayList<>();
    private FollowUpRecordClassifyFragmentAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        client = new OkHttpClient.Builder().
//                 readTimeout(30, TimeUnit.SECONDS).
//                writeTimeout(30, TimeUnit.SECONDS).
        connectTimeout(6, TimeUnit.SECONDS).build();//用于设置请求超时等限制时间

        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(getString(R.string.api))
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        api = retrofit.create(Api.class);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentFollowUpRecordClassifyBinding.inflate(inflater, container, false);
        initData();
        // Inflate the layout for this fragment
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
        return binding.getRoot();
    }

    private void initData() {
        idcard = getArguments().getString("idCard");
        Call<ResponseBody<List<String>>> call = api.getOnesFollowUpType(idcard);


        NOW_DATA.clear();
        binding.recy.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

        call.clone().enqueue(new Callback<ResponseBody<List<String>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<String>>> call, Response<ResponseBody<List<String>>> response) {
                ResponseBody<List<String>> responseBody = response.body();
                if (responseBody != null || responseBody.getData() != null) {
                    NOW_DATA.clear();
                    for (int i = 0; i < responseBody.getData().size(); i++) {
                        NOW_DATA.add(responseBody.getData().get(i));
                        Log.e("initData", "onResponse: "+NOW_DATA.get(i));
                    }
                    adapter=new FollowUpRecordClassifyFragmentAdapter(NOW_DATA,idcard);
                    binding.recy.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<String>>> call, Throwable t) {
                Log.e("initData", "onFailure: "+t);
                if (getContext()!=null){
                    Toast.makeText(getContext(), "网络连接有误", Toast.LENGTH_LONG).show();
                }
                adapter=new FollowUpRecordClassifyFragmentAdapter(NOW_DATA,idcard);
                binding.recy.setAdapter(adapter);
            }
        });

    }

    public void onResume() {
        BottomNavigationView bottomNavigationView = requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);
        //这两行代码设置隐藏底部导航栏，若要显示啧修改INVISIBLE为Visible（可见）
        super.onResume();
    }
}